package com.shundr.auth.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.shundr.auth.base.BaseListViewFragment;
import com.shundr.auth.common.view.CListView.OnLoadMoreListener;
import com.shundr.auth.common.view.CListView.OnRefreshListener;
import com.shundr.auth.frame.constant.LContext;
import com.shundr.auth.frame.http.LHttpRequest;
import com.shundr.auth.frame.util.ToastHelper;
import com.shundr.auth.user.adapter.AuthCompanyItemAdapter;
import com.shundr.auth.user.http.UserRequest;
import com.shundr.auth.user.model.AuthCompanyInfo;

public class AuthCompanyListFragment extends BaseListViewFragment {

	private static final int PAGE_SIZE = LContext.PAGE_SIZE;
	private int pageNo = 1;

	private AuthCompanyItemAdapter mAdapter;

	private List<AuthCompanyInfo> mList = new ArrayList<AuthCompanyInfo>();

	private int status = 0;

	public AuthCompanyListFragment(int status) {
		this.status = status;
	}

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UserRequest.GET_AUTH_LIST_SUCCESSED:
				List<AuthCompanyInfo> infos = (List<AuthCompanyInfo>) msg.obj;
				if (infos != null) {
					if (infos.size() > 0) {
						if (pageNo == 1) {
							mList.clear();
							mListView.setVisibility(View.VISIBLE);
							mLayoutNoContent.setVisibility(View.GONE);
							if (infos.size() >= PAGE_SIZE) {
								mListView.setCanLoadMore(true);
								mListView.setAutoLoadMore(true);
								mListView.setCanRefresh(true);
							} else {
								mListView.setCanLoadMore(false);
								mListView.setAutoLoadMore(false);
								mListView.setCanRefresh(false);
							}
						}
						mList.addAll(infos);
						mAdapter.notifyDataSetChanged();
						if (pageNo == 1) {
							mListView.setSelection(0);
						}
						pageNo = pageNo + 1;
					} else {
						if (pageNo == 1) {
							mListView.setVisibility(View.GONE);
							mLayoutNoContent.setVisibility(View.VISIBLE);
						} else {
							ToastHelper.toastLong(mContext, "最后一页");
						}
					}
				}
				// ProgressDialogHelper.dismiss();
				mListView.onRefreshComplete();
				mListView.onLoadMoreComplete();
				break;
			case LHttpRequest.REQUEST_FAILED:
				mListView.onLoadMoreComplete();
				mListView.onRefreshComplete();
				ToastHelper.toastShort(mContext, msg.obj.toString());
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mAdapter = new AuthCompanyItemAdapter(mList, mContext);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				AuthCompanyInfo info = mList.get(position - 1);
				Intent intent = new Intent(mContext,
						AuthCompanyDetailActivity.class);
				intent.putExtra("id", info.getCompanyId());
				startActivityForResult(intent, 50);
			}

		});
		//
		// mListView.setCanLoadMore(true);
		// mListView.setAutoLoadMore(true);
		// mListView.setCanRefresh(true);

		mListView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				pageNo = 1;
				new UserRequest(mContext, mHandler).getAuthCompanyList(status,
						pageNo, PAGE_SIZE);
			}
		});
		mListView.setOnLoadListener(new OnLoadMoreListener() {
			@Override
			public void onLoadMore() {
				new UserRequest(mContext, mHandler).getAuthCompanyList(status,
						pageNo, PAGE_SIZE);
			}
		});
		pageNo = 1;
		new UserRequest(mContext, mHandler).getAuthCompanyList(status, pageNo,
				PAGE_SIZE);
	}

	public void onRefresh() {
		pageNo = 1;
		new UserRequest(mContext, mHandler).getAuthCompanyList(status, pageNo,
				PAGE_SIZE);
	}

}
